/**
 * Copyright 2007-2008 Vidar Svansson
 *     
 * This file is part of JAdapter.
 * 
 * JAdapter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * JAdapter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with JAdapter.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jadapter.registry;

import java.util.Collection;
import java.util.HashSet;

import org.jadapter.Adapter;
import org.jadapter.exceptions.AdaptationException;
import org.jadapter.graph.Graph;

/**
 * Data structure to store and retrieve adapter instances.
 * <br></br><br></br>
 * This is currently only accessible internally. The modifiers might be promoted to public
 * if a use case demands.
 * 
 * @author Vidar Svansson
 * @since 0.2
 *
 * @param <T>
 */
public class AdapterContainer<T> implements InterfaceProvider {

	private Graph<Class<?>, T> graph;
	private Graph<Class<?>, T> implicitGraph;

	protected AdapterContainer() {
		this(new Graph(), new Graph());
	}

	protected AdapterContainer(Graph<Class<?>, T> graph,
			Graph<Class<?>, T> implicitGraph) {
		this.graph = graph;
		this.implicitGraph = implicitGraph;

	}

	protected final void setGraphs(Graph<Class<?>, T> graph,
			Graph<Class<?>, T> implicitGraph) {
		this.graph = graph;
		this.implicitGraph = implicitGraph;
	}

	@SuppressWarnings("unchecked")
	protected final void register(Adapter<?, ?>... adapters) {
		for (Adapter<?, ?> adapter : adapters) {
			T t = insertAdapter(adapter);
			graph.insert(adapter.to(), adapter.from(), t);
			implicitGraph.insert(adapter.to(), adapter.from(), t);

			for (Class<?> to : adapter.to().getInterfaces()) {
				implicitGraph.insert(to, adapter.from(), t);
				for (Class<?> from : adapter.from().getInterfaces()) {
					implicitGraph.insert(adapter.to(), from, t);
					implicitGraph.insert(to, from, t);
				}
			}
		}
	}

	protected T insertAdapter(Adapter<?, ?> adapter) {
		return (T) adapter;
	}

	protected final T findAdapter(Object context, Class type) {

		Class<?> klass = context.getClass();
		T t = null;

		while (t == null && klass != null) {
			t = graph.get(type, klass);
			if (t == null) {
				t = implicitGraph.get(type, klass);
			}
			if (t == null) {
				for (Class<?> iface : klass.getInterfaces()) {
					// todo check graph first
					t = implicitGraph.get(type, iface);
					if (t != null) {
						break;
					}
				}

			}
			klass = klass.getSuperclass();
		}

		if (t == null)
			throw new AdaptationException("Did not find any transformer");

		return t;

	}

	public final Class<?>[] interfaces(Class<?> ... ifaces) {
		Collection<Class<?>> interfaces = new HashSet<Class<?>>();

		for (Class<?> iface : ifaces) {
			interfaces.add(iface);

			for (Class<?> klass : implicitGraph.to(iface)) {
				if (klass.isInterface())
					interfaces.add(klass);

				for (Class<?> toInterface : klass.getInterfaces())
					interfaces.add(toInterface);
			}
		}
		return interfaces.toArray(new Class<?>[0]);
	}

}
